package exercicio1;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/exercicio1/multiplicacao")
public class Multiplicacao extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		executar(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		executar(request, response);
	}

	private void executar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("multiplicando") == null || request.getParameter("multiplicando").isEmpty()) {
			request.setAttribute("erro", true);
			request.getRequestDispatcher("/exercicio1/index.jsp").forward(request,
					response);
		} else {
			Integer multiplicando = Integer.parseInt(request
					.getParameter("multiplicando"));
			request.setAttribute("multiplicando", multiplicando);
			request.setAttribute("multiplicadores", Arrays.asList(new Integer[] {
					1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }));
			request.getRequestDispatcher("/exercicio1/resultado.jsp").forward(
					request, response);
		}
	}

}
